package util;

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

/**
 * @author rpitts@gmu.edu
 */
public class ObjectPush {

		
	static public void dataPush (Serializable message, String IP, int port) {

		InetAddress ia = null;
		try {
			ia = InetAddress.getByName(IP);
		} catch (UnknownHostException e) {
			e.printStackTrace();
		}

		// Push data
		try {
			if (ia.isReachable(5000)) {
				transmitData(message, IP, port); 
			}
			else {
				System.out.println("IP: " + IP + "port: " + port);
				System.out.println("Send failed--isReachable timed out");
			}
			
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


//} // dataPush
	
	
	static private void transmitData (Serializable data, String servSock, int servPort){ 

		Socket sock = null;
		
		try {
			sock = new Socket(servSock,servPort);
			ObjectOutputStream oos = new ObjectOutputStream(sock.getOutputStream());        
			oos.writeObject(data);
			oos.flush();
			oos.close();
			sock.close();

		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}        

	} // transmit Data
	
} // class ObjectPush
